<template>
  <div class="container">
    <h2 class="th">新闻界面</h2>
    <!-- 
        v-model="activeName"  绑定值，选中选项卡的 name
        @tab-click="handleClick"  tab 被选中时触发	被选中的标签 tab 实例

        label	选项卡标题
        name	与选项卡绑定值 value 对应的标识符，表示选项卡别名

     -->
    <div class="wrapper">
      <div class="left"></div>
      <div class="center">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="要闻" name="1">
            <ul class="list">
              <li v-for="item in yaowenList" :key="item.docid">
                <img :src="item.imgsrc" alt="" />
                <div class="info">
                  <div class="title">{{ item.title }}</div>
                  <div class="desc">
                    来源：{{ item.source }} <span>时间：{{ item.ptime }}</span>
                  </div>
                  <div class="read">阅读：{{ item.commentCount }}</div>
                </div>
              </li>
            </ul>
            <!-- 显示分页器 -->
            <MyPagination
              :total="total"
              :pageSize="pageSize"
              @getPage="getPage"
            />
          </el-tab-pane>
          <el-tab-pane label="国内" name="2">
            <!-- 无限滚动 -->
            <ul class="list" ref="my_pull">
              <li v-for="item in guoneiList" :key="item.docid">
                <img :src="item.imgsrc" alt="" />
                <div class="info">
                  <div class="title">{{ item.title }}</div>
                  <div class="desc">
                    来源：{{ item.source }} <span>时间：{{ item.ptime }}</span>
                  </div>
                  <div class="read">阅读：{{ item.commentCount }}</div>
                </div>
              </li>
            </ul>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="right"></div>
    </div>
  </div>
</template>

<script>
import { yaowen, guonei } from "../../api/type";
import MyPagination from "../../components/MyPagination.vue";
export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      activeName: "1",
      yaowenList: [],
      guoneiList: [],
      total: 10,
      pageSize: 1,
      clientHeight: 0,
      el: {},
      page:1,
      flag:true,//开关
    };
  },
  methods: {
    handleClick(tab, event) {
      // console.log(tab, event);
    },
    //要闻的---点击分页获取数据
    getPage(num) {
      this.$api.getNews(yaowen, num).then((res) => {
        this.yaowenList = res;
        document.documentElement.scrollTop = 0;
      });
    },
    //加载更多数据
    load() {
      // this.count += 2
      console.log("加载更多数据");
    },
    //滚动加载更多数据-------
    handleScroll() {
      let a = this.el.getBoundingClientRect().bottom;
      a = Math.ceil(a);
      if (a <= this.clientHeight && this.flag) {
        this.flag =false;
        this.page++;
        // this.getServer();
        console.log("滚动加载更多数据-",this.page);
      }
    },
  },
  
  created() {
    //1. 要闻数据---------------------
    this.$api.getNews(yaowen, 1).then((res) => {
      console.log("要闻数据", res);
      this.yaowenList = res;
    });

    //2. 国内新闻---------------------
    this.$api.getNews(guonei, 1).then((res) => {
      console.log("国内新闻", res);
      this.guoneiList = res;
    });
  },
  mounted() {
    this.clientHeight = document.documentElement.clientHeight;
    console.log("this.clientHeight", this.clientHeight);
    this.$nextTick(() => {
      this.el = this.$refs.my_pull;
      window.addEventListener("scroll", this.handleScroll);
    });
  },
};
</script>

<style lang='less' scoped>
.container {
  margin: 20px auto;
  width: 1200px;
}
.th {
  margin-bottom: 20px;
}
.wrapper {
  display: flex;
  .center {
    width: 780px;
  }
  .left {
    width: 200px;
    min-height: 600px;
    background: #f5f5f5;
    margin-right: 10px;
  }
  .right {
    margin-left: 10px;
    width: 200px;
    min-height: 600px;
    background: #f5f5f5;
  }
}
.list {
  li {
    display: flex;
    margin-bottom: 20px;
    border-bottom: 1px solid #eee;
    padding-bottom: 10px;
  }
  img {
    width: 140px;
    height: 88px;
    margin-right: 10px;
  }
  .info {
    height: 88px;
    position: relative;
    flex: 1;
    .title {
      color: #404040;
      font-size: 20px;
    }
    .title:hover {
      color: #f34540;
    }
    .desc {
      position: absolute;
      left: 10px;
      bottom: 0;
      color: #888;
      font-size: 12px;
      span {
        margin-left: 30px;
      }
    }
    .read {
      color: #888;
      font-size: 12px;
      position: absolute;
      right: 30px;
      bottom: 0px;
    }
  }
}
/deep/ .el-tabs__header {
  background: #f7f9fa;
  border-top: 1px solid #f04343;
  height: 50px;
  line-height: 50px;
  .el-tabs__active-bar {
    height: 0;
  }
  .el-tabs__item {
    padding: 0;
    height: 50px;
    line-height: 50px;
    display: block;
    float: left;
    width: 65px;
    text-align: center;
    font: normal 16px/50px "Microsoft Yahei";
  }
  .el-tabs__item.is-active {
    background: #f04343;
    font-weight: bold;
    color: #fff;
  }
}
</style>